package com.cqie.rbac.util;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.cqie.rbac.entity.Dict;
import com.cqie.rbac.entity.Quota;
import com.cqie.rbac.entity.QuotaOptions;
import com.cqie.rbac.mapper.DictMapper;
import com.cqie.rbac.mapper.QuotaMapper;
import com.cqie.rbac.mapper.QuotaOptionsMapper;
import com.cqie.rbac.util.ExcelEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 夏兵
 * @Date 2021/9/30
 */
@Component
public class TotalListener extends AnalysisEventListener<ExcelEntity> {

    @Autowired
    private DictMapper dictMapper;
    @Autowired
    private QuotaMapper quotaMapper;
    @Autowired
    private QuotaOptionsMapper quotaOptionsMapper;
    private long dict;
    private long parentId1, parentId2, parentId3, parentId4, parentId5;
    private List<Integer> hasOption = new ArrayList<>();
    private int count = 1, quotaCount = 4;

    /**
     * excel文件解析数据
     * @param excelTotal 每一行数据
     * @param analysisContext
     */
    @Override
    public void invoke(ExcelEntity excelTotal, AnalysisContext analysisContext) {
        if (StringUtils.isNotBlank(excelTotal.getName1()) && NumberUtils.isParsable(excelTotal.getWight1())) {
            saveDict(excelTotal.getName1());
            this.parentId1 = saveData(excelTotal.getName1(), Float.parseFloat(excelTotal.getWight1()), 0);
        }
        if (StringUtils.isNotBlank(excelTotal.getName2()) && NumberUtils.isParsable(excelTotal.getWight2())) {
            this.parentId2 = saveData(excelTotal.getName2(), Float.parseFloat(excelTotal.getWight2()), parentId1);
        }
        if (StringUtils.isNotBlank(excelTotal.getName3())) {
            if (NumberUtils.isParsable(excelTotal.getWight3())) {
                this.parentId3 = saveData(excelTotal.getName3(), Float.parseFloat(excelTotal.getWight3()), parentId2);
            } else {
                saveOptions(excelTotal.getName3(), this.parentId2);
            }
        }
        if (StringUtils.isNotBlank(excelTotal.getName4())) {
            if (NumberUtils.isParsable(excelTotal.getWight4())) {
                this.parentId4 = saveData(excelTotal.getName4(), Float.parseFloat(excelTotal.getWight4()), parentId3);
            } else {
                saveOptions(excelTotal.getName4(), this.parentId3);
            }
        }
        if (StringUtils.isNotBlank(excelTotal.getName5())) {
            if (NumberUtils.isParsable(excelTotal.getWight5())) {
                this.parentId5 = saveData(excelTotal.getName5(), Float.parseFloat(excelTotal.getWight5()), parentId4);
            } else {
                saveOptions(excelTotal.getName5(), this.parentId4);
            }
        }
        if (StringUtils.isNotBlank(excelTotal.getName6())) {
            if (NumberUtils.isParsable(excelTotal.getWight6())) {
                saveData(excelTotal.getName6(), Float.parseFloat(excelTotal.getWight6()), parentId5);
            } else {
                saveOptions(excelTotal.getName6(), this.parentId5);
            }
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        Quota quota = new Quota();
        quota.setHasOption(1);
        for (Integer hasoption : hasOption) {
            quota.setId(hasoption);
            quotaMapper.updateById(quota);
        }
    }

    public void saveDict(String name) {
        Dict dict = new Dict();
        dict.setName(name);
        dictMapper.insert(dict);
        this.dict = dict.getId();
        count = 1;
    }

    public long saveData(String name, float weight, long parent) {
        Quota quota = new Quota();
        quota.setQuotaName(name);
        quota.setWeight(weight);
        quota.setDictId(dict);
        quota.setParentId(parent);
        quota.setSortCode(count++);
        quotaMapper.insert(quota);
        return quota.getId();
    }

    public void saveOptions(String name, long quotaId ) {
        QuotaOptions quotaOptions = new QuotaOptions();
        if (quotaCount < 1) {
            quotaCount = 4;
        }
        float score = 10 * quotaCount--;
        quotaOptions.setOptionScore(score);
        quotaOptions.setOptionName(name);
        quotaOptions.setQuotaId(quotaId);
        hasOption.add(Integer.parseInt(String.valueOf(quotaId)));
        quotaOptionsMapper.insert(quotaOptions);
    }
}
